﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class SubPolicyDiff : System.Web.UI.Page
    {
        protected int subId = 0;
        public static Module_Page Pages = new Module_Page();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterUser"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["subId"] != null && Request.QueryString["subId"] != string.Empty)
            {
                subId = int.Parse(Request.QueryString["subId"]);
            }
            if (!IsPostBack)
            {
                LoadList();
                BindingDrpDiff();
            }
        }
        protected void lbLeve_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    DifficInfo dic = Diffic.GetDifficInfoById(int.Parse(lb.Text));
                    if (dic != null && dic.Id != 0)
                    {
                        lb.Text = dic.Name;
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }

        protected void LoadList()
        {
            try
            {
                TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                if (sub != null && sub.Id !=0)
                {
                    XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                    if (xmlQuestion != null)
                    {
                        this.UserLists.DataSource = xmlQuestion.Diffs;
                        this.UserLists.DataBind();
                    }
                }
            }
            catch
            {
                
            }
        }

        void BindingDrpDiff()
        {
            IList<DifficInfo> list = Diffic.GetDifficInfos(0, int.MaxValue);
            this.drpDiff.DataTextField = "Name";
            this.drpDiff.DataValueField = "Id";
            this.drpDiff.DataSource = list;
            this.drpDiff.DataBind();

        }
        protected void lbDelete_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            if (lb != null && subId!=0)
            {
                try
                {
                    string diffId = lb.CommandArgument;
                    TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                    if (sub != null && sub.Id != 0 && diffId != "")
                    {
                        XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                        if (xmlQuestion.Diffs != null)
                        {
                            for (int i = 0; i < xmlQuestion.Diffs.Count(); i++)
                            {
                                XmlDiff diff = xmlQuestion.Diffs[i];
                                if (diff.DiffId == diffId)
                                {
                                    xmlQuestion.Diffs.Remove(diff);
                                    break;
                                }
                            }
                        }
                        sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                        TestPaperPolicySub.Edit(sub);

                        if (sub != null && sub.Id !=0)
                        {
                            if (xmlQuestion != null)
                            {
                                this.UserLists.DataSource = xmlQuestion.Diffs;
                                this.UserLists.DataBind();
                            }
                            string value = diffId;
                            this.ClientScript.RegisterStartupScript(this.GetType(), "key", " RemoveSelect('BookMaster','lbDiff','" + value + "');", true);
                        }
                        else
                        {
                            this.ClientScript.RegisterStartupScript(this.GetType(), "key", "OnSucceeded('failure')", true);

                        }

                    }
                }
                catch
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "key", "OnSucceeded('failure')", true);
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                if (sub != null && sub.Id != 0 && drpDiff.SelectedIndex != -1)
                {
                    XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                    if (xmlQuestion.Diffs == null)
                    {
                        xmlQuestion.Diffs = new List<XmlDiff>();
                    }
                    XmlDiff diff = new XmlDiff
                    {
                        DiffOptionType = this.drpOptype.Items[drpOptype.SelectedIndex].Value,
                        DiffId = this.drpDiff.SelectedValue,

                        DiffTypePercent = double.Parse(this.drpPercent.Items[this.drpPercent.SelectedIndex].Value)
                    };
                    var query = from item in xmlQuestion.Diffs
                                where item.DiffId == this.drpDiff.SelectedValue
                                select item;
                    if (query.Count() == 0)
                    {
                        xmlQuestion.Diffs.Add(diff);
                        sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                       TestPaperPolicySub.Edit(sub);
                        if (sub != null && sub.Id !=0)
                        {
                            if (xmlQuestion != null)
                            {
                                this.UserLists.DataSource = xmlQuestion.Diffs;
                                this.UserLists.DataBind();
                            }
                            string text = "难度" + this.drpOptype.Items[this.drpOptype.SelectedIndex].Text + this.drpDiff.SelectedItem.Text + "占" + string.Format("{0:0%}", this.drpPercent.Items[this.drpPercent.SelectedIndex].Text);
                            string value = this.drpDiff.SelectedValue;
                            ClientScript.RegisterStartupScript(this.GetType(), "key", " AddToSelect('BookMaster','lbDiff','" + value + "','" + text + "');", true);
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript( this.GetType(), "key", " OnSucceeded('failure');", true);
                        }
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript( this.GetType(), "key", " OnSucceeded('exist');", true);
                    }
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "key", " OnSucceeded('failure');", true);
            }
        }
    }
}

